package com.example.movie.ui.movie.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.movie.R
import com.example.movie.common.constants.AppConstants
import com.example.movie.ui.home.tabs.model.ActorInfo
import kotlinx.android.synthetic.main.actors_list.view.*

/**
 * recycleView适配器
 */
class ActorListAdapter(
    private val items: List<ActorInfo>,
) :
    RecyclerView.Adapter<ActorListAdapter.MyViewHolder>() {

    class MyViewHolder(view: View) :
        RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.actors_list, parent, false)
        )
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        with(holder.itemView){
            items[position].apply {
                actor_name.text = actorName
                role_name.text = roleName
                Glide
                    .with(context)
                    .load(AppConstants.STATIC_SOURCE_URL + imageUrl)
                    .placeholder(R.mipmap.logo)
                    .error(R.drawable.error)
                    .into(actor_image)
            }

        }

    }

    override fun getItemCount() = items.size
}
